package com.ruoyi.web.controller.system.app;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.service.IAppUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * app用户Controller
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/system/appUser")
public class AppUserController extends BaseController
{
    private String prefix = "system/user";

    @Autowired
    private IAppUserService appUserService;


    /**
     * 查询app用户列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppUser appUser)
    {
        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 导出app用户列表
     */
    @Log(title = "app用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppUser appUser)
    {
        appUser.setType(0);
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        return util.exportExcel(list, "app用户数据");
    }


    /**
     * 新增保存app用户
     */
    @Log(title = "app用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppUser appUser)
    {
        AppUser app = appUserService.getAppUserByUserName(appUser.getUserName());
        if (app==null){
            appUser.setType(0);
            appUser.setCreateTime(new Date(System.currentTimeMillis()));
            appUser.setUpdateTime(new Date(System.currentTimeMillis()));
            return toAjax(appUserService.insertAppUser(appUser));
        }else {
            return AjaxResult.error("该账号已注册");
        }
    }


    /**
     * 修改保存app用户
     */
    @Log(title = "app用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppUser appUser)
    {
        appUser.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 删除app用户
     */
    @Log(title = "app用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(appUserService.deleteAppUserByIds(ids));
    }

    /**
     * 用户统计
     * @return
     */
    @PostMapping( "/statistical")
    @ResponseBody
    public AjaxResult statistical(){
        JSONObject object =  appUserService.statistical();
        return AjaxResult.success(object);
    }
}
